package homework5;

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Hashtable;

class WordsHashTable {
 
    public static void main (String[] args) {
	
	try{
		if (args == null || args.length <1)
		    //if no input
		    msgForInvalidInput();
		else if(!isInteger(args[0]))
		    //if input not integer
		    msgForInvalidInput();
		else
		    //valid input
		    System.out.println(integerAsWords(Integer.parseInt(args[0])));
		    
	}catch (NumberFormatException e){
	    msgForInvalidInput();
	}
    }
    
    public static boolean isInteger(String input){
	return Pattern.compile("[0-9]*").matcher(input).matches();
    }
    
    public static void msgForInvalidInput(){
	//Standard error message as per specification
        System.out.println("Usage: words n (where 0 < n < 1000)");
        System.exit(1);
    }
  
    public static String integerAsWords(int n){
	
	Hashtable<Integer,String> hTable=new Hashtable<Integer,String>();
	
	hTable.put(new Integer(1), "one");
	hTable.put(new Integer(2), "two");
	hTable.put(new Integer(3), "three");
	hTable.put(new Integer(4), "four");
	hTable.put(new Integer(5), "five");
	hTable.put(new Integer(6), "six");
	hTable.put(new Integer(7), "seven");
	hTable.put(new Integer(8), "eight");
	hTable.put(new Integer(9), "nine");
	hTable.put(new Integer(10), "ten");
	hTable.put(new Integer(11), "eleven");
	hTable.put(new Integer(12), "twelve");
	hTable.put(new Integer(13), "thirteen");
	hTable.put(new Integer(14), "fourteen");
	hTable.put(new Integer(15), "fifteen");
	hTable.put(new Integer(16), "sixteen");
	hTable.put(new Integer(17), "seventeen");
	hTable.put(new Integer(18), "eighteen");
	hTable.put(new Integer(19), "nineteen");
	hTable.put(new Integer(20), "twenty");
	hTable.put(new Integer(30), "thirty");
	hTable.put(new Integer(40), "forty");
	hTable.put(new Integer(50), "fifty");
	hTable.put(new Integer(60), "sixty");
	hTable.put(new Integer(70), "seventy");
	hTable.put(new Integer(80), "eighty");
	hTable.put(new Integer(90), "ninety");
	hTable.put(new Integer(100),"hundred");
	hTable.put(new Integer(1000),"thousand");
	hTable.put(new Integer(1000000),"million");
	hTable.put(new Integer(1000000000),"billion");
	
	if(n>=1000000000){ // billion
	    
		if(n%1000000000 ==0 ){
		    return integerAsWords(n/1000000000) + " " + hTable.get(1000000000);
		}else {
		    return integerAsWords(n/1000000000) + " " + hTable.get(1000000000) + ", " + integerAsWords(n%1000000000);
		}
	    
	}else if(n>=1000000){ // million
	    
		if(n%1000000 ==0 ){
		    return integerAsWords(n/1000000) + " " + hTable.get(1000000);
		}else {
		    return integerAsWords(n/1000000) + " " + hTable.get(1000000) + ", " + integerAsWords(n%1000000);
		}
	    
	}else if(n>=10000){ //five digit input 
	    
		if(n%10000 ==0 ){
		    return integerAsWords(n/1000) + " " + hTable.get(1000);
		}else {
		    if(n%1000 <=100 ){
			return integerAsWords(n/1000) + " " + hTable.get(1000) + " and " + integerAsWords(n%1000);
		    }else {
			return integerAsWords(n/1000) + " " + hTable.get(1000) + ", " + integerAsWords(n%1000);
		    }
		    
		}
		
	}else if(n>=1000){ //four digit input

	    if (n%1000 == 0) {
		return "" + integerAsWords(n/1000) + " " + hTable.get(1000);
	    } else{	   
		
		if (n%1000 <= 100){
		    return "" + integerAsWords(n/1000) + " " + hTable.get(1000) + " and " + integerAsWords(n%1000);
		}else{
		    
		    if(n%100 ==0){
			return "" + integerAsWords(n/1000) + " " + hTable.get(1000) + " and " + integerAsWords(n%1000);
		    }else {
			return "" + integerAsWords(n/1000) + " " + hTable.get(1000) + ", " + integerAsWords(n%1000);
		    }

		}
	    }
	    
	}else if( n >= 100 ){ // three digit input
	    
	    if (n%100 == 0) 
		return "" + integerAsWords(n/100) + " " + hTable.get(100);
	    else	   
		return "" + integerAsWords(n/100) + " " + hTable.get(100) + " and " + integerAsWords(n%100);
			    
	}else if( n >= 20){
	    
	    if (n%10 == 0){
		return hTable.get(n);
	    }else {
		return hTable.get(n-n%10) + "-" + integerAsWords(n%10);
	    }
	    
	}else {
	    
	    return hTable.get(n);
	}
	
	
    }
}
